package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析logic组件、action组件配置文件中“逻辑结点”的解析类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 谷钰
 * @author 温建
 * @version 1.0 2008.8.18
 * 
 * @see <code>LogicComponentNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.LogicComponentNodeInfo;

public class LogicComponentNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_LOGIC = "logic";

	public static final String KEY_NODE_START_TRANSACTION = "transaction";

	public static final String KEY_LOGIC_TRANSACTION_TYPE = "type";

	public static final String TRANSACTION_TYPE_DEFAULT = "supports";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> logicComponentElements = root
				.elements(KEY_NODETYPE_LOGIC);

		List<INodeInfo> logicComponentNodeInfos = new ArrayList<INodeInfo>(
				logicComponentElements.size());

		for (Element logicComponentElement : logicComponentElements) {
			String key = logicComponentElement.elementTextTrim(KEY_NODE_KEY);

			try {
				String id = logicComponentElement.attributeValue(KEY_NODE_ID)
						.trim();

				String x = logicComponentElement.attributeValue(KEY_NODE_X)
						.trim();
				String y = logicComponentElement.attributeValue(KEY_NODE_Y)
						.trim();
				String name = logicComponentElement
						.elementTextTrim(KEY_NODE_NAME);

				String description = logicComponentElement
						.elementTextTrim(KEY_NODE_DESCRIPTION);

				String nextId = logicComponentElement.attributeValue(
						KEY_NODE_NEXT).trim();

				String exceptionId = logicComponentElement.attributeValue(
						KEY_NODE_EXCEPTION).trim();

				Element transaction = logicComponentElement
						.element(KEY_NODE_START_TRANSACTION);

				String transactionType = null;

				if (transaction != null) {
					transactionType = transaction
							.attributeValue(KEY_LOGIC_TRANSACTION_TYPE);
				}

				if (transactionType == null || "".equals(transactionType)) {
					transactionType = TRANSACTION_TYPE_DEFAULT;
				}
				logger.debug("  load logic node id = \"" + id + "\" key = \"" + key + "\" name = \"" 
						+ name + "\" description = \"" + description
						+ "\" nextId = \"" + nextId + "\"");

				List<IParameterInfo> parameters = getParameterInfos(bundle,
						logicComponentElement);

				LogicComponentNodeInfo endNodeInfo = new LogicComponentNodeInfo(
						id, name, description, nextId, parameters, key,
						exceptionId, transactionType, x, y);

				logicComponentNodeInfos.add(endNodeInfo);

			} catch (Exception e) {
				// 如果解析xml出现异常，对整个xml文件的解析将没有意思，所以不在继续解析
				throw new SaturnComponentException(
						"load logic component node failed! the key is [" + key
								+ "]", e);
			}
		}

		return logicComponentNodeInfos;
	}
}
